﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Repositories;
using YaAppoint.Appointments;
using YaAppoint.Appointments.CheckIn;
using YaAppoint.PrintTemplates;
using YaAppoint.Response;
using YaAppoint.Web.Controllers.Model;

namespace YaAppoint.Web.Controllers
{
    public class CheckInController(ICheckInAppService checkInAppService,
                                   IRepository<PrintTemplate,Guid> printTempalteRepository,
                                   IRepository<Appointment,Guid> appointmentRepository) : Controller
    {
        private readonly ICheckInAppService _checkInAppService = checkInAppService;
        private readonly IRepository<Appointment, Guid> _appointmentRepository= appointmentRepository;
        private readonly IRepository<PrintTemplate, Guid> _printTempalteRepository = printTempalteRepository;

        /// <summary>
        /// PACS端签到成功后回写
        /// </summary>
        /// <param name="appointmentIds"></param>
        /// <returns></returns>
        [AllowAnonymous]      
        [HttpPost("/api/check-in/write-back")]
        public async Task<ActionResult> WriteBackAsync([FromBody]List<string> appointNumbers)
        {
            return new JsonResult(await _checkInAppService.WriteBackAsync(appointNumbers));
        }


        // 向pacs提供签到数据的接口
        [AllowAnonymous]
        [HttpPost("/api/check-in/pull-apply-item-appointment-info")]
        public async Task<ActionResult> PushApplyItemAppointmentData([FromBody] List<string> appointNumbers)
        {
            return new JsonResult(await _checkInAppService.PushApplyItemAppointmentData(appointNumbers));
        }


        /// <summary>
        /// 预约平台端签到
        /// </summary>
        /// <param name="appointmentIds"></param>
        /// <returns></returns>
        [HttpGet("/api/check-in/check-in")]
        public async Task<ActionResult> CheckIn(string appointNumber)
        {
            var appoint = await _appointmentRepository.FirstOrDefaultAsync(a => a.Number == appointNumber);
            if (appoint == null)
            {
                throw new Volo.Abp.BusinessException(CustomErrorCodes.AppointmentNotExist);
            }
            var device = appoint.Pool.Device;
            var printTemplate = await _printTempalteRepository.FirstOrDefaultAsync(p => p.DepartmentId == device.DepartmentId
                                                                                        && p.DeviceTypeId == device.DeviceTypeId
                                                                                        && p.IsActive);
            if (printTemplate == null)
            {
                throw new BusinessException(CustomErrorCodes.PrintTemplateNotExist);
            }
            var remoteCheckInRes = await _checkInAppService.CheckInAsync(appoint.Number);
            if (remoteCheckInRes.Succeed)
            {
                appoint.Status = AppointmentStatus.Achieve;
                await _appointmentRepository.UpdateAsync(appoint);
                remoteCheckInRes.TemplateJsonStr = printTemplate!.JsonContent;
            }
            
            var res = new BackJson
            {
                code = 0,
                data = remoteCheckInRes
            };
            return new JsonResult(res);
        }


        /// <summary>
        /// 加载预约列表
        /// </summary>
        /// <param name="model"></param>
        [HttpPost("/api/check-in/get-appointment-list")]
        public async Task<ActionResult> GetAppointmentList([FromBody] SearchModel model)
        {
            return new JsonResult(await _checkInAppService.GetAppointmentList(model.Number,model.UserData));
        }


    }
}
